<?php
class RegisterForm extends CFormModel
{
	public $username;
	public $email;
	public $password1;
	public $password2;
	
	public function rules()
	{
		return array(
		// username and password are required
				array('username,email, password1,password2', 'required'),
				// password needs to be authenticated
// 				array('password1', 'authenticate1'),
				array('username', 'authenticate'),
				//array('email', 'authenticate'),
				
				
		);
	}
	
// 	public  function authenticate1($attribute,$params)
// 	{
// 		if(!$this->hasErrors())
// 		{
// 			if($this->password1 != $this->password2)
// 				$this->addError('password1','password need be same');
// 		}
// 	}
	
	public function authenticate($attribute,$params)
	{
		if(!$this->hasErrors())
		{
			$user = User::model()->find('userName=?', array($this->username));
			
			if($user != null)
			{
				$this->addError('username','username has been used');
			}
			
			if($this->password1 != $this->password2)
			{
				$this->addError('password1','password need to be same');
			}
// 			$sql = 'select * from tbl_user where userName = '.$this->username .';';
// 			$connection=Yii::app()->db;
// 			$command=$connection->createCommand($sql);
// 			$row=$command->queryRow();
// 			if($row)
// 			{
// 				$this->addError('userName','username has been used');
// 				return;
// 			}
		}
	}
	
	/**
	 * Generates a salt that can be used to generate a password hash.
	 * @return string the salt
	 */
	public function generateSalt()
	{
		return uniqid('',true);
	}
	
	public function hashPassword($password,$salt)
	{
		return md5($salt.$password);
	}
	
	/*
	 * userType : 2 means common ; 1 means admin; 3 means demo
	 */
	public function register()
	{
// 		$this->performAjaxValidation($model);
		//insert into tbl_user
		$salt = $this->generateSalt();
		$pwd = $this->hashPassword($this->password1,$salt);
		
// 		$sql = 'INSERT INTO tbl_user (userType, userName, userEmail, password, salt, accountBalance, created, isAuth) VALUES ('
// 					. '2,\'' . $this->username . ' \' , ', 'f5776b075cb3ea6f3f70a6936e06ba16', '4f5b638ca74948.67618910', 0, '2012-03-10 14:23:02', b'1');
 		$user = new User;
 		$user->userName = $this->username;
 		$user->userType = 2;
 		$user->userEmail = $this->email;
 		$user->password = $pwd;
 		$user->salt = $salt;
 		$user->accountBalance = 0;
 		$user->isAuth = 0;
 		$user->save();
 		//send email
 		$activateLink = 'http://www.articlewritebee.com/index.php/user/activate?x='.$this->username.'&y='.$salt;
 		$to = $this->email;
 		$subject = 'welcome join articlewritebee';
 		$body = '<html><body>Hi '.$this->username .'<br/>' .'please click the link below to activate your account <br/>'
 				. '<a href='.$activateLink.'>'.$activateLink.'</a><br/>Yours<br/><a href=www.articlewritebee.com>articlewritebee</a></body></html>';
 		$headers  = 'MIME-Version: 1.0' . "\r\n";
 		$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
 		$headers .= 'From: ArticleWriteBee <net.fly@articlewritebee.com>';
 		mail($to,$subject,$body,$headers);
// 		$connection=Yii::app()->db;
// 		$command=$connection->createCommand($sql);
// 		$command->execute();
// 		return true;
	}
	
	public function performAjaxValidation($model)
	{
		$user = User::model()->find('userName=?', array($this->username));
		
		if($user != null)
		{
			echo CActiveForm::validate($model);
        	Yii::app()->end();
		}
	}
}